<?php

namespace backend\controllers;

use Yii;
use common\models\Activity\Activity;
use common\models\Activity\ActivitySearch;
use common\models\customInfo\CustomInfo;
use common\models\customInfo\CustomInfoSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\adminlog\AdminLog;
use zhongkefu\zhongkefu;
use zhongkefu\helpers\Utils;

/**
 * ActivityController implements the CRUD actions for Activity model.
 */
class ActivityController extends BackendController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Activity models.
     * @return mixed
     */
    public function actionIndex()
    {
      
        $model = new Activity();
        $searchModel = new ActivitySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Activity model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        $model->start_time = date('Y-m-d H:i:s', $model->start_time);
        $model->end_time = date('Y-m-d H:i:s', $model->end_time);
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Creates a new Activity model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Activity();
        if ($model->load(Yii::$app->request->post())) {

            $model->start_time = strtotime(Yii::$app->request->post()['Activity']['start_time']);
            $model->end_time = strtotime(Yii::$app->request->post()['Activity']['end_time']);

            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->ac_id]);
            } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
        } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
        }
    }

    /**
     * Updates an existing Activity model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post())) {
            $model->start_time = strtotime(Yii::$app->request->post()['Activity']['start_time']);
            $model->end_time = strtotime(Yii::$app->request->post()['Activity']['end_time']);
            if($model->save()){
                return $this->redirect(['view', 'id' => $model->ac_id]);
            } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Activity model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

    /**
     * Finds the Activity model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Activity the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Activity::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }



        /**
     * 发布按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStart()
    {
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $id){
                $model = Activity::findOne(['ac_id' => $id]);
                Activity::updateAll(['ac_status'=>Activity::ACSTATUS_ACTIVE],['ac_id' =>$model->ac_id]);
            }

            return true;
        }
    }
        /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {
        $artname="";
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $val){
                $models = Activity::findOne(['ac_id' => $val]);
                $artname .= $models->ac_name.",";
            }
            $artname = substr($artname,0,-1);   
            foreach($ids as $id){
                $model = Activity::findOne(['ac_id' => $id]);
                $this->findModel($id)->delete();

            }

            Adminlog::addlog(yii::$app->user->id, '批量删除活动 '.$artname);
            return true;
        }
    }
    //已结束
    public function actionUncheck(){
       
        $searchModel = new ActivitySearch();
        $dataProvider = $searchModel->unchecksearch(Yii::$app->request->queryParams);
        return $this->render('uncheck', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    //进行中
    public function actionCheck(){
         //echo "111";die();
        $searchModel = new ActivitySearch();
        $dataProvider = $searchModel->checksearch(Yii::$app->request->queryParams);
        return $this->render('check', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    //未进行
    public function actionWaitcheck(){
        $searchModel = new ActivitySearch();
        $dataProvider = $searchModel->waitchecksearch(Yii::$app->request->queryParams);
        return $this->render('waitcheck', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    //查看报名信息
    public function actionBaominginfo($id){
        $model = new Activity();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['baomingview', 'id' => $model->id]);
        } else {
            return $this->render('baomingview', [
                'model' => $model,
            ]);
        }
    }

}
